//
//  MagazineViewController.m
//  B_WatchHome
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 DHF. All rights reserved.
//

#import "MagazineViewController.h"
#import "MagazineDataViewController.h"
#import "MagazineModel.h"
#import "MagazineTableViewCell.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import <Masonry.h>
#import "AppTools.h"
#import <UIImage+GIF.h>

@interface MagazineViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *myTableView;

@end

@implementation MagazineViewController

//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    // 还有东西没写
//    
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"腕表杂志";
    
    // 创建tableView
    self.myTableView = [[UITableView alloc] init];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    // 约束
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.right.and.bottom.mas_equalTo(0);
    }];
    
    // 轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    self.myTableView.tableHeaderView = [[UIView alloc] init];
    [self.myTableView.tableHeaderView addGestureRecognizer:tap];
    
    [self.myTableView registerClass:[MagazineTableViewCell class] forCellReuseIdentifier:@"cell"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.bigArray = [NSMutableArray array];
    self.keysArray = [NSMutableArray array];
    self.headArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    self.i = 1;
    
    [self getAFNetWorkingData];
    
    

    [self addFooterRefresh];
    
//    [self addHeaderRefresh];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    NSString *name = @"header.gif";
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImage *image = [UIImage sd_animatedGIFWithData:imageData];
    
    NSArray *arr = [NSArray arrayWithObjects:image, nil];
    
    [header setImages:arr forState:MJRefreshStateIdle];
    
    [header setImages:arr forState:MJRefreshStatePulling];
    
    [header setImages:arr forState:MJRefreshStateRefreshing];
    
    self.myTableView.mj_header = header;
    
}


- (void)loadNewData
{
    NSLog(@"下拉");
    
    self.i = 1;
    
    //  重新请求数据
    [self getAFNetWorkingData];
    
    [self.myTableView.mj_header beginRefreshing];
}


// 上拉
//- (void)addHeaderRefresh
//{
//    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        NSLog(@"下拉");
//        
//        self.i = 1;
//        
//        //  重新请求数据
//        [self getAFNetWorkingData];
//        
//    }];
//    [self.myTableView.mj_header beginRefreshing];
//    }

- (void)addFooterRefresh
{
    // 当上拉时触发该方法
    self.myTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       
        NSLog(@"上拉");
        
        self.i++;
        
        [self getAFNetWorkingData];
        
    }];

    
    
    

}





- (void)getAFNetWorkingData
{
    NSString *urkStr = [NSString stringWithFormat:@"http:watch-cdn.idailywatch.com/api/list/magazine/zh-hans?page=%ld&ver=iphone&app_ver=10", self.i];
    
    if (self.i == 1) {
        [AppTools getDataWithURL:urkStr Block:^(id result) {
           
            [self.keysArray removeAllObjects];
            [self.headArray removeAllObjects];
            self.bigArray = result;
            for (NSMutableDictionary *dic in self.bigArray) {
                MagazineModel *model = [[MagazineModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                if (model.guid != 14373 &&![model.cat isEqualToString:@"more"]) {
                    [self.keysArray addObject:model];
                }else{
                    [self.headArray addObject:model];
                    
                }
                [self.allArray addObject:model];
            }
            
            MagazineModel *model = [self.headArray objectAtIndex:0];
            
            UIImageView *imageView = [[UIImageView alloc] init];
            NSURL *url = [NSURL URLWithString:model.cover_thumb];
            [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]];
            
            
            
            [self.myTableView reloadData];
            // 上下拉刷新没加
            [self.myTableView.mj_header endRefreshing];
            [self.myTableView.mj_footer endRefreshing];
            
            NSLog(@"11111");
            
            
        }];
    }else{
        
        [AppTools getDataWithURL:urkStr Block:^(id result) {
           
            self.bigArray = result;
            for (NSMutableDictionary *dic in self.bigArray) {
                MagazineModel *model = [[MagazineModel alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                if (model.guid != 14373 &&![model.cat isEqualToString:@"more"]) {
                    [self.keysArray addObject:model];
                }else{
                    [self.headArray addObject:model];
                    
                }
                
                [self.allArray addObject:model];
            }
            
            [self.myTableView reloadData];
            
            // 上下拉刷新
            
            [self.myTableView.mj_header endRefreshing];
            [self.myTableView.mj_footer endRefreshing];
            
            
        }];
    
    }
}

//- (void)showTabBar
//{
//    if (self.tabBarController.tabBar.hidden == NO) {
//        return;
//    }
//    UIView *contentView;
//    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]) {
//        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
//    }else{
//        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
//        contentView
//    
//    }
//}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"这是模态详情页");
    MagazineDataViewController *dataVC = [[MagazineDataViewController alloc] init];
    MagazineModel *model = [self.headArray objectAtIndex:0];
    
    //model赋值
    dataVC.model = model;
    
    [self.navigationController pushViewController:dataVC animated:YES];
 
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.keysArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MagazineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    MagazineModel *model = [self.keysArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MagazineModel *model = [self.keysArray objectAtIndex:indexPath.row];
    
    MagazineDataViewController *dataVC = [[MagazineDataViewController alloc] init];
    // model 赋值
    dataVC.model = model;
    
    
    [self.navigationController pushViewController:dataVC animated:YES];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
